package com.app.core.configuration;


import com.app.core.mark.CraftsmanConfiguration;
import com.app.core.mvc.repeat.RepeatCommitInterceptor;
import com.app.core.mvc.returnvalue.ResultRespBodyHandleReturnValue;
import com.app.core.mvc.serialization.argument.EncryptLongPathVariableMethodArgumentResolver;
import com.app.core.mvc.serialization.argument.EncryptLongRequestParamMethodArgumentResolver;
import com.app.core.mvc.serialization.argument.GetRequestBodyMethodArgumentResolver;
import com.app.core.signature.SignatureInterceptor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.List;

/**
 * @author qiangt
 * @since 2023/6/30 19:42
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class SpringMvcAutoConfiguration implements WebMvcConfigurer, CraftsmanConfiguration {

    private final RepeatCommitInterceptor repeatCommitInterceptor;

    private final SignatureInterceptor signatureInterceptor;

    private final String[] excludePaths = new String[]{
            "/test/**",
    };

    @PostConstruct
    public void init() {
        log.info("配置 SpringMvcAutoConfiguration");
    }

    @Override
    public void addInterceptors(final InterceptorRegistry registry) {
        registry.addInterceptor(this.repeatCommitInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(Arrays.asList(excludePaths));
        registry.addInterceptor(signatureInterceptor).addPathPatterns("/**");
    }

    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        // 添加静态资源过滤
        // knife4j 增强 swagger 配置 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        // knife4j 增强 swagger 配置 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");// 静态资源路径 css,js,img等
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
    }

    @Override
    public void addArgumentResolvers(final List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(new EncryptLongPathVariableMethodArgumentResolver());
        resolvers.add(new EncryptLongRequestParamMethodArgumentResolver());
        resolvers.add(new GetRequestBodyMethodArgumentResolver());
    }

    @Override
    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> handlers) {
        handlers.add(new ResultRespBodyHandleReturnValue());
    }
}
